package vsm.vsm_server.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import vsm.vsm_server.mapper.PredictMapper;
import vsm.vsm_server.pojo.Predict.Predict;
import vsm.vsm_server.pojo.User.Record.History.WatchDTO;
import vsm.vsm_server.pojo.User.Record.Like.LikeVideo;
import vsm.vsm_server.pojo.User.VisitDTO;
import vsm.vsm_server.pojo.Video.Video;
import vsm.vsm_server.service.ActionService;
import vsm.vsm_server.service.VideoService;

import java.math.BigInteger;

@Service
@Slf4j
public class ActionServiceImpl implements ActionService {
    private PredictMapper predictMapper;

    @Autowired
    VideoService videoService;
    public ActionServiceImpl(PredictMapper predictMapper){this.predictMapper=predictMapper;}
    @Override
    @Async
    public void LikeVideo(LikeVideo likeVideo) {

        log.info("喜欢视频，{}",likeVideo);
        Predict predict = new Predict();
        BigInteger tagId = videoService.getVideo(likeVideo.getVideoId()).getTagId();
        if(tagId==null){
            tagId = BigInteger.valueOf(-1);
        }
        predict.setTagId(tagId);
        predict.setLabel(1);
        predict.setVideoId(likeVideo.getVideoId());
        predict.setUserId(likeVideo.getUserId());
        predictMapper.savePredict(predict);
    }

    @Override
    @Async
    public void SubscribeUser(BigInteger userid, BigInteger followid, Integer relation) {
        log.info("关注用户");

    }

    @Override
    @Async
    public void VisitUser(VisitDTO visitDTO) {
        log.info("访问主页,{}",visitDTO);

    }



  //观看视频行为记录
    @Override
    @Async
    public void WatchVideo(WatchDTO watchDTO) {
        log.info("观看视频,{}",watchDTO);
        if(watchDTO.getNewVideo()){ //新观看视频才记录观看行为
            Double interest = watchDTO.getCurrentTime()/ watchDTO.getDuration();
            if(watchDTO.getPlayTime()>=watchDTO.getDuration())
                interest = 1.0;
            if(interest>=0.3) { //观看率达到30%
                Predict predict = new Predict();
                predict.setUserId(watchDTO.getUserId());
                predict.setVideoId(watchDTO.getVideoId());
                Video video = videoService.getVideo(watchDTO.getVideoId());
                BigInteger tagId = video.getTagId();
                if(tagId==null){
                    tagId = BigInteger.valueOf(-1);
                }
                predict.setTagId(tagId);
                int label = 0;
                if(interest>=0.6){ //观看率达到60%
                    label = 1;
                }
                predict.setLabel(label);

                predictMapper.savePredict(predict);
            }
        }

    }
}
